package cc.zy.base.hr.service.impl;

import cc.zy.base.businesses.entity.ErrorLog;
import cc.zy.base.businesses.entity.NetworkSchool;
import cc.zy.base.businesses.utils.ReadAccountExcel;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.FebsResponse;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import cc.zy.base.hr.entity.DraftPlan;
import cc.zy.base.hr.entity.DraftPlanSchedule;
import cc.zy.base.hr.entity.HrDept;
import cc.zy.base.hr.mapper.DraftPlanMapper;
import cc.zy.base.hr.mapper.DraftPlanScheduleMapper;
import cc.zy.base.hr.mapper.HrDeptMapper;
import cc.zy.base.hr.service.IDraftPlanScheduleService;
import cc.zy.base.hr.service.IDraftPlanService;
import cc.zy.base.system.entity.Dept;
import cc.zy.base.system.mapper.DeptMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author YF
 * @time 2022/03/09 14:27:45
 */

@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class DraftPlanServiceImpl extends ServiceImpl<DraftPlanMapper, DraftPlan> implements IDraftPlanService {


    @Resource
    private final DraftPlanMapper draftPlanMapper;

    @Resource
    private final DeptMapper deptMapper;
    @Resource
    private final HrDeptMapper hrDeptMapper;


    @Resource
    private final IDraftPlanScheduleService draftPlanScheduleService;

    @Resource
    private final DraftPlanScheduleMapper draftPlanScheduleMapper;



    @Override
    public IPage<DraftPlan> findDrafts(QueryRequest request, DraftPlan draftPlan) {
        Page<DraftPlan> page = new Page<>(request.getPageNum(), request.getPageSize());
        Integer integer = draftPlanMapper.countDraftPlanDetail(draftPlan);
        page.setTotal(integer);
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        log.debug("拿到的草稿页面信息是："+page);
        return baseMapper.findDraftPlanDetailPage(page, draftPlan);
    }

    @Override
    public DraftPlan findDraftDetailList(Integer id) {

        DraftPlan param = new DraftPlan();
        param.setId(id);
        List<DraftPlan> drafts = baseMapper.findDraftPlanDetail(param);
        return CollectionUtils.isNotEmpty(drafts) ? drafts.get(0) : null;
    }

    @Override
    public DraftPlan findById(Integer id) {

        return draftPlanMapper.findById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DraftPlan> findTags(DraftPlan draft) {

        LambdaQueryWrapper<DraftPlan> queryWrapper = new LambdaQueryWrapper<>();
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createDraft(DraftPlan draft) {
        //至少一个
        if (draft.getCheckStyleOperation()==null){
            draft.setCheckStyleOperation((byte) 0);
        }
        if (draft.getCheckStyleExperience()==null){
            draft.setCheckStyleExperience((byte) 0);
        }
        if (draft.getCheckStyleCommunacation()==null){
            draft.setCheckStyleCommunacation((byte) 0);
        }
        if (draft.getChechStyleExamine()==null){
            draft.setChechStyleExamine((byte) 0);
        }

        String organizationDeptNameList = draft.getOrganizationDeptNameList();
        String[] split = organizationDeptNameList.split(",");
        StringBuffer s1 =new StringBuffer();

        for (int i = 0; i < split.length; i++) {
            HrDept dept = hrDeptMapper.findById(Integer.valueOf(split[i]));
            s1.append(dept.getDeptName()+";");

        }
        System.out.println(s1.toString());

        draft.setOrganizationDeptNameList(s1.toString());
        draft.setCreateDate(new Date());
        draft.setLastModifyDate(new Date());
        this.save(draft);
        Date date = new Date();
        Integer id = draft.getId();
        List<Date> planDate = draft.getPlanDateAdd();


        DraftPlanSchedule draftSchedule = null;

        for (int i = 0; i < planDate.size(); i++) {

            log.debug(planDate.get(i).toString());
            draftSchedule = new DraftPlanSchedule();
            draftSchedule.setTrainningPlanDraftId(id);
            //魔法值
            draftSchedule.setPeriodType(11000);
            Date date1 = planDate.get(i);

            System.out.println(planDate.get(i));

            draftSchedule.setPlanDate(planDate.get(i));
            log.debug(String.format("draftSchedule", draftSchedule));
            draftPlanScheduleService.createDraftSchedule(draftSchedule);
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDraft(DraftPlan draft) {
        draft.setLastModifyDate(new Date());
        this.saveOrUpdate(draft);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletedraft(DraftPlan draft) {

        LambdaQueryWrapper<DraftPlan> wrapper = new LambdaQueryWrapper<>();
        this.remove(wrapper);

    }

    @Override
    public void deleteDrafts(String[] draftIds) {

        List<String> list = Arrays.asList(draftIds);
        // 删除标签
        this.removeByIds(list);
        List<Integer> resultDraftIds = list.stream().map(s -> Integer.valueOf(s)).collect(Collectors.toList());

        draftPlanScheduleMapper.removeByDraftPlanIds(resultDraftIds);

    }

    @Override
    public List<DraftPlan> findDraftListNotPage(DraftPlan draft) {
        List<DraftPlan> draftPlanListNotPage = baseMapper.findDraftPlanListNotPage(draft);
        return draftPlanListNotPage;
    }



    @Override
    public Boolean isRepetitive(String name) {
        List<DraftPlan> repetitive = draftPlanMapper.isRepetitive(name);
        Boolean flag = true;
        if (repetitive != null && repetitive.size() > 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public String readExcel(MultipartFile multipartFile, HttpSession session) {
        //未完成
        return null;

    }
}
